package session01.homework;

import javax.swing.*;
import java.sql.*;
import java.util.Scanner;

public class TestHomeWork {
    private static String url = "jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8";
    private static String user = "root";
    private static String pass = "123456";

    private static boolean light = false;

    public static void main(String[] args) {
        String name = JOptionPane.showInputDialog(null, "请输入用户名");
        String pwd = JOptionPane.showInputDialog(null, "请输入密码");

        Scanner input = new Scanner(System.in);
        login(name, pwd);
        if (light) {
            String num = JOptionPane.showInputDialog(null, "请输入你实现的功能");
            switch (num) {
                case "1":
                    System.out.println("请输入你要查询的id：");
                    int id1 = input.nextInt();
                    query(id1);
                    break;
                case "2":
                    System.out.println("请输入你要存款的id：");
                    int id2 = input.nextInt();
                    System.out.println("请输入你要存款的金额：");
                    double account2 = input.nextDouble();
                    store(id2, account2);
                    break;
                case "3":
                    System.out.println("请输入你要修改的人的id：");
                    int id3 = input.nextInt();
                    System.out.println("请输入旧密码：");
                    String old = input.next();
                    System.out.println("请输入新密码：");
                    String now = input.next();
                    changePwd(id3, old, now);
                    break;
                case "4":
                    System.out.println("请输入你自己的id：");
                    int id4 = input.nextInt();
                    System.out.println("请输入你要转账的人的id：");
                    int id5 = input.nextInt();
                    System.out.println("请输入金额");
                    double account3 = input.nextDouble();
                    trans(id4, id5, account3);
                    break;
                default:
                    System.out.println("你输入的数字无意义");
            }
        } else {
            String temp = JOptionPane.showInputDialog(null, "请输入余额");
            //将String转化为Double
            Double account = Double.parseDouble(temp);
            register(name, pwd, account);
        }
    }

    /*
        登陆
     */
    public static void login(String username, String password) {
        String sql = "select f_username,f_password from t_user where f_username= ? and f_password= ?";
        try (Connection con = DriverManager.getConnection(url, user, pass);
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            boolean flag = false;
            while (rs.next()) {
                flag = true;
                light = true;
            }
            if (flag) {
                JOptionPane.showMessageDialog(null, "登陆成功");
            } else {
                JOptionPane.showMessageDialog(null, "登陆失败");
                JOptionPane.showMessageDialog(null, "你输入的信息将被存入数据库");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
        注册
     */
    public static void register(String username, String password, double account) {
        String sql = "insert into t_user(f_username,f_password,f_account) values (?,?,?)";
        try (Connection con = DriverManager.getConnection(url, user, pass);
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.setString(2, password);
            ps.setDouble(3, account);

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
        查询余额
     */
    public static void query(int userid) {
        String sql = "select f_account from t_user where pk_userid = ?";
        try (Connection con = DriverManager.getConnection(url, user, pass);
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setInt(1, userid);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                double money = rs.getDouble("f_account");
                System.out.println(money);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
        存款
     */
    public static void store(int userid, double money) {
        String sql = "update t_user set f_account=f_account + ? where pk_userid = ?";
        try (Connection con = DriverManager.getConnection(url, user, pass);
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setDouble(1, money);
            ps.setInt(2, userid);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
        修改密码
     */
    public static void changePwd(int userid, String oldPwd, String newPwd) {
        String sql1 = "select f_username from t_user where f_password =? and pk_userid=?";
        String sql2 = "update t_user set f_password=? where pk_userid=?";
        try (Connection con = DriverManager.getConnection(url, user, pass);
             PreparedStatement ps1 = con.prepareStatement(sql1);
             PreparedStatement ps2 = con.prepareStatement(sql2)) {
            ps1.setString(1, oldPwd);
            ps1.setInt(2, userid);
            //1、先查看旧密码是否正确
            ResultSet rs = ps1.executeQuery();
            while (rs.next()) {
                System.out.println("旧密码正确");
                //2、如果正确再来做修改
                ps2.setString(1, newPwd);
                ps2.setInt(2, userid);
                ps2.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
        转账
     */
    public static void trans(int fromID, int toID, double money) {
        String sql1 = "update t_user set f_account=f_account-? where pk_userid=?";
        String sql2 = "update t_user set f_account=f_account+? where pk_userid=?";
        try (Connection con = DriverManager.getConnection(url, user, pass)) {
            con.setAutoCommit(false);

            try (PreparedStatement ps1 = con.prepareStatement(sql1);
                 PreparedStatement ps2 = con.prepareStatement(sql2)) {
                ps1.setDouble(1, money);
                ps1.setInt(2, fromID);
                ps1.executeUpdate();

                ps2.setDouble(1, money);
                ps2.setInt(2, toID);
                ps2.executeUpdate();

                con.commit();
            } catch (SQLException e) {
                con.rollback();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
